home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MASM.ZIP / SAMPLES / INCLUDE / PROLOGUE.IN$ / PROLOGUE.bin
Encoding:
Text File  |  1991-02-28  |  7.2 KB  |  335 lines

  1. pushcontext listing
  2. .nolist
  3. ;
  4. ;    Copyright Microsoft ------ finish copyright message here
  5.  
  6. ;    This file is used to create the same sets of prologue and epilogue
  7. ;    sequences which the Microsoft C 6.00 compiler will produce.  This
  8. ;    file would be used for writing windows programs and to provide
  9. ;    such features as stack checking in the assembler portions of
  10. ;    a C based project.
  11.  
  12.  
  13. ;    The following global variables will affect the prolog/epilog
  14. ;    sequences produced
  15. ;
  16. ;    ?WP_DEBUG - If 1 then prolog/epilog sequences will be forced
  17. ;    ?WP_CHECKSTACK - If 1 then a check stack will be forced on all
  18. ;            procedures
  19. ;    ?WP_INCBP - If 1 then the inc bp sequence will be generated on
  20. ;        all far procedures
  21. ;    ?WP_LOADDS - If 1 then the load ds sequence will be generated on
  22. ;        all far procedures
  23. ;
  24. ifndef ?WP_DEBUG
  25. ?WP_DEBUG = 0
  26. endif
  27. ifndef ?WP_CHECKSTACK
  28. ?WP_CHECKSTACK = 0
  29. endif
  30. ifndef ?WP_INCBP
  31. ?WP_INCBP = 0
  32. endif
  33. ifndef ?WP_LOADDS
  34. ?WP_LOADDS = 0
  35. endif
  36.  
  37. ;
  38. ;    Complain if we are in a segment as this will affect how the
  39. ;    externdefs are done and therefore the fixups and code
  40. ;    created on the checkstack calls
  41. ;
  42. % ifnb    <@curseg>
  43. echo    Include should not be contained in a segment
  44. endif
  45.  
  46. externdef C    _aNchkstk:near        ; Extern the symbols
  47. externdef C    _aFchkstk:far        ; for later reference
  48.  
  49. ;
  50. ;    This macro will produce the same output as will the 
  51. ;    C6 compiler for the given switches.
  52. ;
  53. ;    The following may be placed in the MacroArgs field of the
  54. ;        proc defintion:
  55. ;
  56. ;    CHECKSTACK
  57. ;    NOCHECKSTACK
  58. ;    LOADDS
  59. ;    NOLOADDS
  60. ;    FORCEFRAME
  61. ;    INCBP
  62. ;    NOINCBP
  63.  
  64. cPrologue macro szProcName, flags, cbParams, cbLocals, rgRegs, rgUserParams
  65.     LOCAL    ?doPrologue
  66.     LOCAL    ?loadds
  67.     LOCAL    ?checkstack
  68.     LOCAL    ?incbp
  69. ;    pushcontext    listing
  70. ;    .nolistmacro
  71. ;    .listmacroall
  72.     
  73.     ?doPrologue = 0
  74.     ?loadds = 0
  75.     ?checkstack = 0
  76.     ?incbp = 0
  77.  
  78. ;;    Set the defaults based on the global values specified
  79. ;;
  80. if    ?WP_DEBUG NE 0            ;; Force frames by default
  81.  ?doPrologue = 1
  82. endif
  83.  
  84. if     ?WP_CHECKSTACK NE 0        ;; Force checkstack by default
  85.  ?checkstack = 1
  86. endif
  87.  
  88. if    ?WP_INCBP NE 0            ;; Force incbp by default if far
  89.  if flags AND 020h
  90.   ?incbp = 1
  91.  endif
  92. endif
  93.  
  94. if    ?WP_LOADDS NE 0            ;; Force loadds by default
  95.  ?loadds = 1
  96. endif
  97.  
  98. ;;
  99. ;;    Get all of the user parameters parsed
  100. ;;
  101.  
  102. ifnb    <rgUserParams>        ;;    Parse user params if exsisting
  103.  for p,<rgUserParams>        ;;     For every user param
  104.  
  105.   ifidn <p>, <CHECKSTACK>    ;;     Is it checkstack?
  106.    ?checkstack = 1        ;;        Yes -- do checkstack
  107.   endif
  108.  
  109.   ifidn <p>, <NOCHECKSTACK>    ;;    Don't do checkstack?
  110.    ?checkstack = 0        ;;        Yes -- clear checkstack 
  111.   endif
  112.  
  113.   ifidn <p>, <LOADDS>    ;;    Is it LoadDS
  114.    ?loadds = 1            ;;        Yes -- do loadds sequence
  115.   endif
  116.  
  117.   ifidn <p>, <NOLOADDS>    ;;    Don't do LoadDS?
  118.    ?loadds = 0            ;;        Yes -- clear loadds flag
  119.   endif
  120.  
  121.   ifidn <p>, <INCBP>        ;; Is it IncBP
  122.    if flags AND 020h        ;;      and far?    
  123.     ?incbp = 1            ;;    Yes -- do IncBP sequence
  124.    endif
  125.   endif
  126.  
  127.   ifidn <p>, <NOINCBP>    ;;    Is it NoIncBP
  128.    ?incbp = 0            ;;        Yes -- Clear the incbp flag
  129.   endif
  130.  
  131.   ifidn <p>, <FORCEFRAME>    ;;    Is it ForceFrame?
  132.    ?doPrologue = 1            ;;        Yes -- force out a frame
  133.   endif
  134.  
  135.  endm            ;; End of user parameter parsing loop
  136. endif
  137.  
  138. ;;  Frames are generated iff
  139. ;;    1. cbLocals + cbParams != 0
  140. ;;    2. FORCEFRAME is set
  141. ;;    3. INCBP is set and proc is far
  142. ;;    4. LOADDS is set
  143. ;;
  144. ;; Force a prolog?    
  145.  
  146. ?doprologue = ?doprologue OR ?incbp OR ?loadds OR ?checkstack OR (cbLocals NE 0) OR (cbParams NE 0)
  147.  
  148. if    ?doPrologue EQ 0    ;; No prolog needed -- so get out of here
  149. ;    popcontext listing
  150.     exitm<0>
  151. endif
  152.  
  153. if    ?loadds EQ 1        ;; Create the loadds code -- force in
  154.     push    ds        ;;    Put DS into AX -- we will place
  155.     pop    ax        ;;    back in DS later.  This sequence
  156.     nop            ;;    is altered by the OS if needed
  157. endif
  158.  
  159. if    ?incbp EQ 1        ;; Mark as a far procedure for stack
  160.     inc    bp        ;;    walking
  161. endif
  162.  
  163.     push    bp        ;; Create the frame
  164.     mov    bp,sp
  165.  
  166. if     ?loadds EQ 1        ;; Load up DS with the value in AX
  167.     push    ds        ;;
  168.     mov    ds,ax        ;;
  169. endif
  170.  
  171. if    ?checkstack EQ 1    ;; Now  allocate space for locals
  172.     mov    ax,cbLocals    ;;     # of bytes of locals
  173. % ifidn    <@CurSeg>, <_TEXT>
  174.     call    _aNchkstk    ;;     Call run time routine to allocate
  175.  else
  176.     call    _aFchkstk
  177.  endif
  178. else    ; ?checkstack NE 1
  179.   if    cbLocals NE 0
  180.     sub    sp,cbLocals    ;;    make space on the stack for locals
  181.   endif
  182. endif
  183.  
  184. ifnb    rgRegs            ;; There are registers to be saved.  do so
  185.     for r,rgRegs
  186.         push    r
  187.     endm
  188. endif
  189. ;    popcontext listing
  190.     exitm <cbLocals>
  191.  
  192. endm
  193.  
  194.  
  195.  
  196. ;
  197. ;    This macro will produce the same output as will the 
  198. ;    C6 compiler for the given switches.
  199. ;
  200. ;    The following may be placed in the MacroArgs field of the
  201. ;        proc defintion:
  202. ;
  203. ;    CHECKSTACK
  204. ;    NOCHECKSTACK
  205. ;    LOADDS
  206. ;    NOLOADDS
  207. ;    FORCEFRAME
  208. ;    INCBP
  209. ;    NOINCBP
  210.  
  211. cEpilogue macro szProcName, flags, cbParams, cbLocals, rgRegs, rgUserParams
  212.     LOCAL    ?doPrologue
  213.     LOCAL    ?loadds
  214.     LOCAL    ?checkstack
  215.     LOCAL    ?incbp
  216. ;    pushcontext    listing
  217. ;    .nolistmacro
  218. ;    .listmacroall
  219.     
  220.     ?doPrologue = 0
  221.     ?loadds = 0
  222.     ?checkstack = 0
  223.     ?incbp = 0
  224.  
  225. ;;    Set the defaults based on the global values specified
  226. ;;
  227. if    ?WP_DEBUG NE 0            ;; Force frames by default
  228.  ?doPrologue = 1
  229. endif
  230.  
  231. if     ?WP_CHECKSTACK NE 0        ;; Force checkstack by default
  232.  ?checkstack = 1
  233. endif
  234.  
  235. if    ?WP_INCBP NE 0            ;; Force incbp by default
  236.  if flags AND 020h
  237.   ?incbp = 1
  238.  endif
  239. endif
  240.  
  241. if    ?WP_LOADDS NE 0            ;; Force loadds by default
  242.  ?loadds = 1
  243. endif
  244.  
  245. ;;
  246. ;;    Get all of the user parameters parsed
  247. ;;
  248.  
  249. ifnb    <rgUserParams>        ;;    Parse user params if exsisting
  250.  for p,<rgUserParams>        ;;     For every user param
  251.  
  252.   ifidn <p>, <CHECKSTACK>    ;;     Is it checkstack?
  253.    ?checkstack = 1        ;;        Yes -- do checkstack
  254.   endif
  255.  
  256.   ifidn <p>, <NOCHECKSTACK>    ;;    Don't do checkstack?
  257.    ?checkstack = 0        ;;        Yes -- clear checkstack 
  258.   endif
  259.  
  260.   ifidn <p>, <LOADDS>    ;;    Is it LoadDS
  261.    ?loadds = 1            ;;        Yes -- do loadds sequence
  262.   endif
  263.  
  264.   ifidn <p>, <NOLOADDS>    ;;    Don't do LoadDS?
  265.    ?loadds = 0            ;;        Yes -- clear loadds flag
  266.   endif
  267.  
  268.   ifidn <p>, <INCBP>    ;;    Is it IncBP
  269.    if flags AND 020h
  270.     ?incbp = 1            ;;        Yes -- do IncBP sequence
  271.    endif
  272.   endif
  273.  
  274.   ifidn <p>, <NOINCBP>    ;;    Is it NoIncBP
  275.    ?incbp = 0            ;;        Yes -- Clear the incbp flag
  276.   endif
  277.  
  278.   ifidn <p>, <FORCEFRAME>    ;;    Is it ForceFrame?
  279.    ?doPrologue = 1            ;;        Yes -- force out a frame
  280.   endif
  281.  
  282.  endm            ;; End of user parameter parsing loop
  283. endif
  284.  
  285. ;;  Frames are generated iff
  286. ;;    1. cbLocals + cbParams != 0
  287. ;;    2. FORCEFRAME is set
  288. ;;    3. INCBP is set and proc is far
  289. ;;    4. LOADDS is set
  290. ;;
  291. ;; Force a prolog?    
  292.  
  293. ?doprologue = ?doprologue OR ?incbp OR ?loadds OR ?checkstack OR (cbLocals NE 0) OR (cbParams NE 0)
  294.  
  295. if    ?doPrologue EQ 0    ;; No epilog needed -- so get out of here
  296.     ret
  297.     exitm
  298. endif
  299.  
  300. ifnb    rgRegs            ;; Pop off the registers -- they are in
  301.     for r,rgRegs        ;; inverse order from the prologue call
  302.         pop    r
  303.     endm
  304. endif
  305.  
  306. if    ?loadds            ;;
  307.     dec    bp
  308.     dec    bp
  309.     mov    sp,bp
  310.     pop    ds
  311.     pop    bp
  312. else
  313.  
  314.     mov    sp,bp
  315.     pop    bp
  316. endif
  317.  
  318. if    ?incbp            ;; Remove the increment of BP if necessary
  319.     dec    bp
  320. endif
  321.  
  322. if     flags AND 010h        ;; Caller pops stack arguments
  323.     ret
  324. else                ;; Callee pops args
  325.  if    cbParams NE 0        ;; Put out the correct form of return
  326.     ret    cbParams
  327.  else
  328.     ret
  329.  endif
  330. endif
  331. endm
  332.  
  333. popcontext    listing
  334. .listmacro
  335.